package com.djsearch.util;

import java.io.IOException;
import java.io.InputStream;
import java.lang.ref.SoftReference;
import java.net.URL;
import java.util.HashMap;

import com.djsearch.data.ImageManager;

import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.os.Message;
import android.widget.ImageView;
import android.widget.ProgressBar;


public class AsyncImageLoader {
	//异步加载图片
	public HashMap<String, SoftReference<Drawable>> imageCache;
	Drawable drawable;
	private String filePath = "";///mnt/sdcard/djs/Photo
    private String pathImg;
	public AsyncImageLoader(String pathImg) {
		imageCache = new HashMap<String, SoftReference<Drawable>>();
		this.pathImg = pathImg;
	}
	
	public Drawable loadDrawable(final ProgressBar progressBar, final ImageView imageView,final String imageUrl,
			final ImageCallback imageCallback) {
		
		final Handler handler = new Handler() {
			public void handleMessage(Message message) {
				imageCallback.imageLoaded(progressBar,imageView,(Drawable) message.obj, imageUrl, imageUrl);
			}
		};
		Drawable drawableFromCache = null;
		if (imageCache.containsKey(imageUrl)) {
			SoftReference<Drawable> softReference = imageCache.get(imageUrl);
		    drawableFromCache = softReference.get();
		}
		//读取sd卡
		if (drawableFromCache == null) {
			Bitmap bitmap = ImageManager.readImgFromSDcard(pathImg, imageUrl);
			if (bitmap != null ) {
				drawable = ImageManager.BitmapConvertToDrawale(bitmap);
			}
		}
		if (drawable != null) {
			Message message = handler.obtainMessage(0, drawable);
			handler.sendMessage(message);
			return drawable;
		}
		new Thread() {
			@Override
			public void run() {
				if(drawable != null){
					drawable = null;
				}
				drawable = loadImageFromUrl(imageUrl);
				if(drawable == null)
					return;
				if (drawable != null) {
					Bitmap bitmap = ImageManager.drawableToBitmap(drawable);
					if (bitmap != null) {
						ImageManager.writeImgToSDcard(pathImg, imageUrl, bitmap);
					}
					if (bitmap != null && !bitmap.isRecycled()) {
						bitmap.recycle();
					}
				}
				imageCache.put(imageUrl, new SoftReference<Drawable>(drawable));
				
				Message message = handler.obtainMessage(0, drawable);
				handler.sendMessage(message);
			}
		}.start();
		return null;
	}

	public void recycleBitmap(Bitmap bitmap) {
		if (bitmap != null && !bitmap.isRecycled()) {
			bitmap.recycle();
		}
	}
	
	public  Drawable loadImageFromUrl(String url) {
		URL m;
		Drawable d = null;
		InputStream i = null;
			try {
				m = new URL(url);
				if(m==null||m.openStream()==null){
					return null;
				}
				i = (InputStream) m.openStream();
			
			} catch (Exception e) {
				e.printStackTrace();
				if(i!=null) {
					try {
						i.close();
					} catch (IOException e1) {
						e1.printStackTrace();
						return null;
					}
				}
				return null;
			}
			try {
				d = Drawable.createFromStream(i, "src");
			} catch (Exception e) {
				e.printStackTrace();
				return null;
			}
			
			if(i!=null) {
				try {
					i.close();
				} catch (IOException e) {
					e.printStackTrace();
					return null;
				}
			}
			return d;
	}

	public interface ImageCallback {
		public void imageLoaded(ProgressBar progressBar,ImageView imageView,
				Drawable imageDrawable, String imageUrl,String tag);
	}

}